package com.hzw.saas.web.admin.rbac.controller;

import java.util.List;

import com.hzw.saas.api.rbac.IPermissionService;
import com.hzw.saas.api.rbac.bo.PermissionTreeBO;
import com.hzw.saas.api.rbac.constants.RbacConsts;
import com.hzw.saas.api.rbac.enums.PermSysEnum;
import com.hzw.saas.api.rbac.enums.PermTypeEnum;
import com.hzw.saas.api.rbac.query.PermissionAddQuery;
import com.hzw.saas.api.rbac.query.PermissionUpdateQuery;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.web.admin.rbac.dto.PermSysDTO;
import com.hzw.saas.web.admin.rbac.dto.PermTypeDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 08/31/2021
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@Api(tags = "授权管理/权限管理")
public class PermissionController {

    private final IPermissionService permissionService;

    @GetMapping("/sys/permission/system")
    @SysLog
    @ApiOperation(value = "获取权限所属系统列表", notes = "获取权限模块列表")
    @ApiOperationSort(11)
    public ResponseEntity<List<PermSysDTO>> listPermissionModules() {
        List<PermSysDTO> permModuleDTOS = MapperUtil.nf().mapAsList(PermSysEnum.values(), PermSysDTO.class);
        return ResponseEntity.ok(permModuleDTOS);
    }

    @GetMapping("/sys/permission/types")
    @SysLog
    @ApiOperation(value = "获取权限类型列表", notes = "获取权限类型列表")
    @ApiOperationSort(12)
    public ResponseEntity<List<PermTypeDTO>> listPermissionTypes() {
        List<PermTypeDTO> permModuleDTOS = MapperUtil.nf().mapAsList(PermTypeEnum.values(), PermTypeDTO.class);
        return ResponseEntity.ok(permModuleDTOS);
    }

    @GetMapping("/sys/permissions")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_PERM_LIST)")
    @SysLog
    @ApiOperation(value = "获取所有权限列表", notes = "获取所有权限列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sys", value = "所属系统, 1:后台管理用户, 2:云平台用户", dataType = "int", paramType = "query")
    })
    @ApiOperationSort(13)
    public ResponseEntity<List<PermissionTreeBO>> listPermissions(Integer sys) {
        List<PermissionTreeBO> permissions = permissionService.listTree(sys, RbacConsts.ROOT_PERMISSION_SUP_ID, -1);
        return ResponseEntity.ok(permissions);
    }

    @PostMapping("/sys/permission")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_PERM_SAVE)")
    @SysLog(operation = "新增权限", mode = 3)
    @ApiOperation(value = "新增权限", notes = "新增权限")
    @ApiOperationSort(14)
    public ResponseEntity<Void> addPermission(@Validated @RequestBody PermissionAddQuery param) {
        permissionService.save(param);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/sys/permission")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_PERM_UPDATE)")
    @SysLog(operation = "更新权限", mode = 3)
    @ApiOperation(value = "更新权限", notes = "更新权限")
    @ApiOperationSort(15)
    public ResponseEntity<Void> updatePermission(@Validated @RequestBody PermissionUpdateQuery param) {
        permissionService.update(param);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/sys/permission/{permissionId}")
    @PreAuthorize("@pms.assertAny(@baseAuth.SYS_PERM_DELETE)")
    @SysLog(operation = "删除权限", mode = 3)
    @ApiOperation(value = "删除权限", notes = "删除权限，会同时删除所有的子权限")
    @ApiOperationSort(16)
    public ResponseEntity<Void> deletePermission(@PathVariable String permissionId) {
        permissionService.delete(permissionId);
        return ResponseEntity.ok().build();
    }

}
